package com.example.matetest.slice.home;

import com.example.matetest.ResourceTable;
import com.example.matetest.constant.Constant;
import com.example.matetest.util.LogUtils;
import de.hdodenhof.circleimageview.CircleImageView;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.ToastDialog;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

public class MyInfoAbilitySlice extends AbilitySlice implements Component.ClickedListener {
    private static final String TAG = MyInfoAbilitySlice.class.getSimpleName();
    private static final HiLogLabel logLabel = new HiLogLabel(HiLog.LOG_APP, 0x00001, TAG);

    private Image imageBack;
    private Text title;

    private CircleImageView circleHead;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_my_info);

        title = (Text) findComponentById(ResourceTable.Id_tv_title);
        imageBack = (Image) findComponentById(ResourceTable.Id_iv_back);
        imageBack.setClickedListener(this);
        title.setText("个人信息");
        initView();

        circleHead = (CircleImageView) findComponentById(ResourceTable.Id_civ_icon);
        circleHead.setClickedListener(this);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_iv_back:
                terminate();
                break;

            case ResourceTable.Id_civ_icon:
                showToast();
                break;

            default:
                break;
        }
    }

    private void initView() {
        HiLog.info(logLabel, "ce...shi>>>...");
    }

    /**
     * Toast
     */
    private void showToast() {
        DirectionalLayout toastLayout = (DirectionalLayout) LayoutScatter.getInstance(this)
                .parse(ResourceTable.Layout_toast_dialog, null, false);

        if (toastLayout == null) {
            LogUtils.debug(TAG, "toastLayout is null");
        } else {
            Text mgsComp = (Text) toastLayout.getComponentAt(0);
            if (mgsComp == null) {
                LogUtils.debug(TAG, "mgsComp is null");
            } else {
                mgsComp.setText(Constant.TOAST_MSG_NO_INTERNET);
            }
        }

        ToastDialog toastDialog = new ToastDialog(getContext())
                .setComponent(toastLayout)
                .setSize(DirectionalLayout.LayoutConfig.MATCH_CONTENT, DirectionalLayout.LayoutConfig.MATCH_CONTENT)
                .setAlignment(LayoutAlignment.CENTER);
        toastDialog.setDuration(Constant.TOAST_DURATION_MS).show();
    }

}
